package com.prp.gwt.client.user.friend;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.Host;
import com.prp.gwt.client.widgets.UserBarPanel;
import com.prp.gwt.client.widgets.user.friend.FriendListPanel;

public class FriendHistoryHandler implements ValueChangeHandler<String> {

	public static final String MYLIST = "mylist";
	public static final String PREFIX_FRIENDLIST = "friendlist=";
	public static final String INIT_VALUE = MYLIST;

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		String token = event.getValue();

		if (token.equals(MYLIST)) {
			UserBarPanel.getInstance().selectTab(ClientConstants.INDEX_FRIEND);
			FriendListPanel.getInstance().init();
			FriendListener.getInstance().getFriendList(-1);
			FriendListener.getInstance().findInvite();
			FriendListener.getInstance().getRecommendList();
		} else if (token.startsWith(PREFIX_FRIENDLIST))
			try {
				int id = Integer.parseInt(token.substring(PREFIX_FRIENDLIST
						.length()));
				UserBarPanel.getInstance().selectTab(ClientConstants.INDEX_FRIEND);
				FriendListPanel.getInstance().init();
				FriendListener.getInstance().getFriendList(id);
				FriendListener.getInstance().findInvite();
				FriendListener.getInstance().getRecommendList();
			} catch (NumberFormatException e) {
				Window.Location.replace(Host.getPAGE_NOT_FOUND());
			}
	}
}
